/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-12-28 09:52:56
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-01-11 16:58:34
 */
export function fontsize(size) {
  let deviceWidth = document.documentElement.clientWidth || window.innerWidth;
  if (deviceWidth >= 750 || deviceWidth == undefined) {
    deviceWidth = 750;
  }
  return (deviceWidth / 750) * size;
}
export function GetXCount(x0, min, x1) {
  var count =
    ((parseInt(x1.split(":")[0]) - parseInt(x0.split(":")[0])) * 60 +
      parseInt(x1.split(":")[1]) -
      parseInt(x0.split(":")[1])) /
    min -
    1;
  return count;
}
export function GetXData(x0, min, count) {
  var xData = new Array();
  xData[0] = x0;
  for (var i = 1; i < count; i++) {
    var stime = xData[xData.length - 1];
    var hour = stime.split(":")[0];
    var minute = stime.split(":")[1];

    var nexthour = parseInt(hour);
    var nextminute = parseInt(minute) + min;
    if (nextminute == 60) {
      nextminute = 0;
      nexthour = parseInt(hour) + 1;
    }
    if (nexthour < 10) {
      hour = "0" + nexthour;
      //hour = nexthour;
    } else {
      hour = nexthour;
    }
    if (nextminute < 10) {
      minute = "0" + nextminute;
    } else {
      minute = nextminute;
    }
    xData[xData.length] = hour + ":" + minute;
  }
  return xData;
}
export function GetXtime() {
  var mytime24 = new Array();
  var i = 0;
  var now_data = new Date();
  var year_now = now_data.getFullYear(),
    month_now = now_data.getMonth() + 1,
    day_now = now_data.getDate();
  var time_pre = `${year_now}/${month_now}/${day_now} 00:00:00`;
  var ttt = new Date(time_pre).getTime();
  for (; i < 25; i++) {
    mytime24.push([ttt, ""]); //传入value的值为空则该值点不会显示在图表中
    ttt = ttt + 3600000;
  }
  return mytime24;
}
/**
 * 排序
 * 根据一个字段排序
 * @param
 */
export function sortCommon(dataList, name) {
  var data = dataList;
  for (let i = 0; i < data.length - 1; i += 1) {
    //通过 this.length 次把第一位放到最后,完成排序
    //-i是因为最后的位置是会动态改变的，当完成一次后,最后一位会变成倒数第二位
    for (let j = 0; j < data.length - 1 - i; j += 1) {
      if (data[j][name] < data[j + 1][name]) {
        const temp = data[j];
        data[j] = data[j + 1];
        data[j + 1] = temp;
      }
    }
  }
  return data;
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 * @param datetime 国际化日期格式
 */
export function format(datetime) {
  return formatWithSeperator(datetime, "/", ":");
}
export function formatX(datetime) {
  return formatWithSeperator(datetime, "-", ":");
}
export function formatY(datetime) {
  return formatWithSeperator2(datetime, "-");
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式
 */
export function formatWithSeperator(datetime, dateSeprator, timeSeprator) {
  if (datetime != null) {
    const dateMat = new Date(datetime);
    const year = dateMat.getFullYear();
    const month = dateMat.getMonth() + 1;
    const day = dateMat.getDate();
    const hh = dateMat.getHours();
    const mm = dateMat.getMinutes();
    const ss = dateMat.getSeconds();
    const timeFormat =
      year +
      dateSeprator +
      (month < 10 ? "0" + month : month) +
      dateSeprator +
      (day < 10 ? "0" + day : day) +
      " " +
      hh +
      timeSeprator +
      mm +
      timeSeprator +
      ss;
    return timeFormat;
  }
}
export function formatWithSeperator2(datetime, dateSeprator) {
  if (datetime != null) {
    const dateMat = new Date(datetime);
    const year = dateMat.getFullYear();
    const month = dateMat.getMonth() + 1;
    const day = dateMat.getDate();
    const timeFormat =
      year +
      dateSeprator +
      (month < 10 ? "0" + month : month) +
      dateSeprator +
      (day < 10 ? "0" + day : day);
    return timeFormat;
  }
}
export function newFormData(info) {
  const form = new FormData();
  for (const key in info) {
    form.append(key, info[key]);
  }
  return form;
}

// 获取当前日期时间
export function getDatetime() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var day = now.getDate();
  var hh = now.getHours();
  var mm = now.getMinutes();
  var ss = now.getSeconds();
  var clock = year + "-";
  if (month < 10) clock += "0";
  clock += month + "-";
  if (day < 10) clock += "0";
  clock += day + " ";
  if (hh < 10) clock += "0";
  clock += hh + ":";
  if (mm < 10) clock += "0";
  clock += mm;
  // + ":"
  // if (ss < 10) clock += "0";
  // clock += ss;
  return clock;
}
// 获取当前日期--只获取到天
export function getDatetimeToDay() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var day = now.getDate();
  var hh = now.getHours();
  var mm = now.getMinutes();
  var ss = now.getSeconds();
  var clock = year + "-";
  if (month < 10) clock += "0";
  clock += month + "-";
  if (day < 10) clock += "0";
  clock += day;
  return clock;
}
//获取近七天日期
export function getSevenDay() {
  var dateList = [];
  for (var i = 0; i < 7; i++) {
    var date = new Date();
    date.setDate(date.getDate() - i);
    // console.log(
    //   date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
    // );
    var a = date.getMonth() + 1 + "-" + date.getDate();
    dateList.push(a);
  }
  dateList = dateList.reverse();
  return dateList;
}

// 获取当前天的开始时间
export function formatDate(date) {
  return [
    date.getFullYear(),
    String(date.getMonth() + 1).padStart(2, '0'),
    String(date.getDate()).padStart(2, '0')
  ].join('-') + ' ' +
    [
      String(date.getHours()).padStart(2, '0'),
      String(date.getMinutes()).padStart(2, '0')
    ].join(':');
}

export function formatDate2(date) {
  return [
    date.getFullYear(),
    String(date.getMonth() + 1).padStart(2, '0'),
    String(date.getDate()).padStart(2, '0')
  ].join('-')
}

